package structuralPattern.flyweight.example02;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂类-生产棋子
 */
public class GobangFactory {

    // 享元池
    private static Map<String,GobangFlyweight> pool;

    // 创建共享享元对象，设置对象的内部状态
    private GobangFactory() {
        pool = new HashMap<>();
        pool.put("W",new WhiteGobang());
        pool.put("B",new BlackGobang());
    }

    // 静态内部类
    private static class SingletonHolder{
        private static final GobangFactory FACTORY = new GobangFactory();
    }

    // 获取工厂单例
    public static GobangFactory getInstance(){
        return SingletonHolder.FACTORY;
    }

    // 获取棋子
    public GobangFlyweight getGobang(String color){
        return pool.get(color);
    }
}
